package com.jjb.www.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.cqjjb.calendarview.Calendar;
import com.cqjjb.calendarview.MonthView;
import com.jjb.www.R;
import com.jjb.www.utils.DpUtil;


/**
 * 高仿魅族日历布局
 * Created by huanghaibin on 2017/11/15.
 */

public class SimpleMonthView extends MonthView {

    private int mRadius;
    /*** 今天的背景色*/
    private final Paint mCurrentDayPaint = new Paint();
    /*** 背景圆点*/
    private final Paint mPointPaint = new Paint();
    /*** 圆点半径*/
    private final float mPointRadius;
    private final int mPadding;

    public SimpleMonthView(Context context) {
        super(context);
        mCurrentDayPaint.setAntiAlias(true);
        mCurrentDayPaint.setStyle(Paint.Style.FILL);
        mCurrentDayPaint.setColor(getContext().getColor(R.color.color_not_select));

        mPointPaint.setAntiAlias(true);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setTextAlign(Paint.Align.CENTER);
        mPointPaint.setColor(Color.RED);

        mPadding = DpUtil.dp2px(1);
        mPointRadius = DpUtil.dp2px(2);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSchemePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        //重新设置选中的颜色
        mSelectedPaint.setColor(getContext().getColor(R.color.color_4181fe));
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return true;
    }

//    public int getStatus(String time){
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            if (time.equals(list.get(i))) {
//               return (true?1:2);
//            }
//        }
//        return 0;
//    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        boolean isSelected = isSelected(calendar);
        if (calendar.isStatus()) {
            mPointPaint.setColor(Color.GREEN);
        } else {
            mPointPaint.setColor(Color.RED);
        }
        int cx = x + mItemWidth / 2;
        Paint.FontMetrics metrics = mCurMonthTextPaint.getFontMetrics();
        //文本的一半高度
        int textHeight = (int) ((metrics.bottom - metrics.top) / 2);
        //圆点的Y坐标
        int cy = y + mItemHeight / 2 + textHeight + mPadding;
        canvas.drawCircle(cx, cy, mPointRadius, mPointPaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        //是今天,且今天未被选择的背景
        if (calendar.isCurrentDay() && !isSelected) {
            canvas.drawCircle(cx, cy, mRadius, mCurrentDayPaint);
        }
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }
}
